<script src=../priorities/resources/common.js></script>
<script src=../resources/testharness.js></script>
<script src=../resources/testharnessreport.js></script>

<!-- Setup getInitialResourcePriority promises. -->
<script>
const priority_tests = [
  {
    promise: internals.getInitialResourcePriority(new URL('../resources/dummy.css', location), document),
    expected_priority: kHigh,
    description: 'low importance on <link rel=preload as=style> must be fetched with kHigh resource load priority'
  },
  {
    promise: internals.getInitialResourcePriority(new URL('../resources/dummy.css?1', location), document),
    expected_priority: kVeryHigh,
    description: 'missing importance on <link rel=preload as=style> must have no effect on resource load priority'
  },
  {
    promise: internals.getInitialResourcePriority(new URL('../resources/dummy.css?2', location), document),
    expected_priority: kVeryHigh,
    description: 'high importance on <link rel=preload as=style> must have no effect on resource load priority'
  },
  {
    promise: internals.getInitialResourcePriority(new URL('../resources/dummy.js', location), document),
    expected_priority: kLow,
    description: 'low importance on <link rel=preload as=script> must be fetched with kLow resource load priority'
  },
  {
    promise: internals.getInitialResourcePriority(new URL('../resources/dummy.js?1', location), document),
    expected_priority: kHigh,
    description: 'missing importance on <link rel=preload as=script> must have no effect on resource load priority'
  },
  {
    promise: internals.getInitialResourcePriority(new URL('../resources/dummy.js?2', location), document),
    expected_priority: kHigh,
    description: 'high importance on <link rel=preload as=script> must have no effect on resource load priority'
  },
  {
    promise: internals.getInitialResourcePriority(new URL('../resources/dummy.txt', location), document),
    expected_priority: kLow,
    description: 'low importance on <link rel=preload as=fetch> must be fetched with kLow resource load priority'
  },
  {
    promise: internals.getInitialResourcePriority(new URL('../resources/dummy.txt?1', location), document),
    expected_priority: kHigh,
    description: 'missing importance on <link rel=preload as=fetch> must have no effect on resource load priority'
  },
  {
    promise: internals.getInitialResourcePriority(new URL('../resources/dummy.txt?2', location), document),
    expected_priority: kHigh,
    description: 'high importance on <link rel=preload as=fetch> must have no effect on resource load priority'
  },
  {
    promise: internals.getInitialResourcePriority(new URL('../resources/square.png', location), document),
    expected_priority: kLow,
    description: 'low importance on <link rel=preload as=image> must have no effect on resource load priority'
  },
  {
    promise: internals.getInitialResourcePriority(new URL('../resources/square.png?1', location), document),
    expected_priority: kLow,
    description: 'missing importance on <link rel=preload as=image> must have no effect on resource load priority'
  },
  {
    promise: internals.getInitialResourcePriority(new URL('../resources/square.png?2', location), document),
    expected_priority: kHigh,
    description: 'high importance on <link rel=preload as=image> must be fetched with kHigh resource load priority'
  }
];
</script>

<!-- as=style -->
<!-- don't need to test for invalid and explicit auto importance here, since we already do that in the other link test -->
<link id=link1 importance=low rel=preload as=style href=../resources/dummy.css>
<link id=link2 rel=preload as=style href=../resources/dummy.css?1>
<link id=link3 importance=high rel=preload as=style href=../resources/dummy.css?2>

<!-- as=script-->
<link id=link4 importance=low rel=preload as=script href=../resources/dummy.js>
<link id=link5 rel=preload as=script href=../resources/dummy.js?1>
<link id=link6 importance=high rel=preload as=script href=../resources/dummy.js?2>

<!-- as=fetch-->
<link id=link7 importance=low rel=preload as=fetch href=../resources/dummy.txt>
<link id=link8 rel=preload as=fetch href=../resources/dummy.txt?1>
<link id=link9 importance=high rel=preload as=fetch href=../resources/dummy.txt?2>

<!-- as=image-->
<link id=link10 importance=low rel=preload as=image href=../resources/square.png>
<link id=link11 rel=preload as=image href=../resources/square.png?1>
<link id=link12 importance=high rel=preload as=image href=../resources/square.png?2>

<script>
  promise_test(async (t) => {
    await new Promise(resolve => {
      addEventListener('DOMContentLoaded', resolve);
    });

    const base_msg = ' was fetched by the preload scanner';
    assert_true(internals.isPreloaded(link1.href), link1.href + base_msg);
    assert_true(internals.isPreloaded(link2.href), link2.href + base_msg);
    assert_true(internals.isPreloaded(link3.href), link3.href + base_msg);
    assert_true(internals.isPreloaded(link4.href), link4.href + base_msg);
    assert_true(internals.isPreloaded(link5.href), link5.href + base_msg);
    assert_true(internals.isPreloaded(link6.href), link6.href + base_msg);
    assert_true(internals.isPreloaded(link7.href), link7.href + base_msg);
    assert_true(internals.isPreloaded(link8.href), link8.href + base_msg);
    assert_true(internals.isPreloaded(link9.href), link9.href + base_msg);
    assert_true(internals.isPreloaded(link10.href), link10.href + base_msg);
    assert_true(internals.isPreloaded(link11.href), link11.href + base_msg);
    assert_true(internals.isPreloaded(link12.href), link12.href + base_msg);
  }, 'all preloads must be fetched by the preload scanner');

  // Setup the tests described by |priority_tests|.
  for (const test of priority_tests) {
    promise_test(async () => {
      const priority = await test.promise;
      assert_equals(priority, test.expected_priority);
    }, test.description);
  }
</script>
